package com.mission.schedule.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.mission.schedule.CommonDialog.NoteTypeNameEditDialog;
import com.mission.schedule.R;
import com.mission.schedule.adapter.NoteTypePXAdapter;
import com.mission.schedule.adapter.utils.CommonAdapter;
import com.mission.schedule.adapter.utils.ViewHolder;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.NoteTypeBean;
import com.mission.schedule.bean.TagCommandBean;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.service.UpdataTagService;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.SharedPrefUtil;
import com.mobeta.android.dslv.DragSortListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class NoteTitleSortActivity extends BaseActivity implements OnClickListener {

    @ViewResId(id = R.id.new_dtl_back)
    private LinearLayout new_dtl_back;
    @ViewResId(id = R.id.top_ll_right)
    private RelativeLayout top_ll_right;
    @ViewResId(id = R.id.tag_lv)
    private DragSortListView tag_lv;
    @ViewResId(id = R.id.myschedule_title)
    private TextView myschedule_title;

    Context context;

    App app = App.getDBcApplication();
    List<NoteTitleDetailBean.TDelListBean> mList = new ArrayList<>();
    List<TagCommandBean> list = new ArrayList<TagCommandBean>();
    String userid;
    SharedPrefUtil sharedPrefUtil = null;
    String filed = "0";
    String localid = "2";

    @Override
    protected void setListener() {
        new_dtl_back.setOnClickListener(this);
        top_ll_right.setOnClickListener(this);
    }

    int type = 0;
    CalenderTypePXAdapter calenderTypePXAdapter;

    @Override
    protected void setContentView() {
        setContentView(R.layout.qdpx);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        context = this;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        userid = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERID, "");
        tag_lv.setDropListener(onDrop);
        myschedule_title.setText("清单排序");

        localid = getIntent().getStringExtra("localid");
        filed = getIntent().getStringExtra("filed");
        mList = app.getFiledNoteTitlesTypeData(userid, filed, localid);

        calenderTypePXAdapter = new CalenderTypePXAdapter(context, mList, R.layout.notetitlesort_item);
        tag_lv.setAdapter(calenderTypePXAdapter);

    }

    @Override
    protected void setAdapter() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.new_dtl_back:
                this.finish();
                break;
            case R.id.top_ll_right:

                int i = 1;

                List<NoteTitleDetailBean.TDelListBean> tagCommandBeanList = calenderTypePXAdapter.list;
                for (NoteTitleDetailBean.TDelListBean bean : tagCommandBeanList) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.SECOND, -i);
                    if (bean.titleId != null) {
                        app.updateNoteTitleSort(DateUtil.formatDateTimeSs(calendar.getTime()), bean.titleId);
                        i++;
                    }
                }


                Intent intent = new Intent();
                setResult(Activity.RESULT_OK, intent);
                this.finish();
                break;
            default:
                break;
        }
    }

    private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
        @Override
        public void drop(int from, int to) {
            if (from != to) {
                NoteTitleDetailBean.TDelListBean item = calenderTypePXAdapter.getItem(from);
                calenderTypePXAdapter.remove(item);
                calenderTypePXAdapter.insert(item, to);
                calenderTypePXAdapter.notifyDataSetChanged();
            }
        }
    };


    class CalenderTypePXAdapter extends CommonAdapter<NoteTitleDetailBean.TDelListBean> {
        public List<NoteTitleDetailBean.TDelListBean> list;

        public CalenderTypePXAdapter(Context context, List<NoteTitleDetailBean.TDelListBean> lDatas, int layoutItemID) {
            super(context, lDatas, layoutItemID);
            this.list = lDatas;
        }


        @Override
        public void getViewItem(ViewHolder holder, NoteTitleDetailBean.TDelListBean item, int position) {
            TextView content_text = (TextView) holder.getView(R.id.content_text);
            if (mList != null) {
                content_text.setText(mList.get(position).titles);

                content_text.setText("");
                int num = App.getDBcApplication().getTitleNoteDetailData(mList.get(position).uid + "", mList.get(position).titleId, false).size();
                String titlestr;
                if (mList.get(position).titles.equals("")) {
                    titlestr = App.getDBcApplication().getNoteTitleDetailContent(mList.get(position).uid + "", mList.get(position).titleId);
                    if (titlestr.isEmpty()) {
                        titlestr = "无标题";
                    } else {
//                        titlestr = titlestr.substring(0, showStringLength(titlestr));
                        if (titlestr.length() > 16) {
                            titlestr = titlestr.substring(0, 16);
                        }
                    }

                } else {
                    titlestr = mList.get(position).titles;
                    if (titlestr.length() > 16) {
                        titlestr = titlestr.substring(0, 16);
                    }
                }

                String nums = "";
                String title_new = "";

                if (mList.get(position).styles == 0) {
                    int tick = App.getDBcApplication().getNoteDetailEndstateNums(mList.get(position).titleId, mList.get(position).uid + "");
                    if (num == 0) {
                        title_new = "";
                    } else {
                        title_new = "(" + tick + "/" + num + ")";
                    }
                } else if (mList.get(position).styles == 4) {
                    int tick1 = App.getDBcApplication().getNoteDetailEndstateNums(mList.get(position).titleId, mList.get(position).uid + "", 1);
                    int tick2 = App.getDBcApplication().getNoteDetailEndstateNums(mList.get(position).titleId, mList.get(position).uid + "", 2);
                    if (num == 0) {
                        title_new = "";
                    } else {
                        title_new = "(" + tick1 + "/" + tick2 + "/" + num + ")";
                    }
                } else {
                    if (num == 0) {
                        title_new = "";
                    } else {
                        title_new = "(" + num + ")";
                    }
                }
                if (mList.get(position).remark3.equals("1")
                        || mList.get(position).remark3.equals("2")) {
                    if (!titlestr.isEmpty()) {
                        titlestr = "#" + titlestr;
                    }
                }
                SpannableString builder = new SpannableString(titlestr);
                builder.setSpan(new ForegroundColorSpan(Color.BLACK), 0, titlestr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                if (titlestr.indexOf("#") == 0) {
                    builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.title0)), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                content_text.append(builder);


                String pname = "@" + mList.get(position).pname;

                if (mList.get(position).remark3.equals("1")
                        || mList.get(position).remark3.equals("2")) {
                    //收藏清单
                    SpannableString builder1 = new SpannableString(pname);
                    builder1.setSpan(new ForegroundColorSpan(Color.parseColor("#aaaaaa")), 0, pname.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    content_text.append(builder1);
                }
//                else {
//                    //收到清单
//                    SpannableString builder1 = new SpannableString(pname);
//                    builder1.setSpan(new ForegroundColorSpan(Color.BLACK), 0, pname.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//                    content_text.append(builder1);
//                    content_text.setMovementMethod(LinkMovementMethod.getInstance());
//                }

                SpannableString builder2 = new SpannableString(title_new);
                ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
                builder2.setSpan(span, 0, title_new.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                content_text.append(builder2);
            }
        }

        public void remove(NoteTitleDetailBean.TDelListBean item) {
            if (item != null && list != null) {
                list.remove(item);
            }
        }

        public void insert(NoteTitleDetailBean.TDelListBean item, int to) {
            if (item != null) {
                list.add(to, item);
            }
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
